package br.com.guiav6.sisguia.dao.hibernate;

import java.util.List;

import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;

import br.com.guiav6.sisguia.dao.UsuarioDao;
import br.com.guiav6.sisguia.entity.Usuario;
import br.com.guiav6.sisguia.repository.hibernate.AbstractDaoHibernate;

@Repository("usuarioDao")
public class UsuarioDaoHibernate extends AbstractDaoHibernate<Long, Usuario> implements UsuarioDao {

	@Override
	public List<Usuario> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Usuario findById(Long id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Usuario findByCredencials(String userName, String password) {
		
		StringBuilder sql = new StringBuilder("from Usuario where username = :userName ");
		sql.append("and password = :password ");
		
		TypedQuery<Usuario> q = getEntityManager().createQuery(sql.toString(), Usuario.class);
		q.setParameter("userName", userName);
		q.setParameter("password", password);
		try{
			return q.getSingleResult();
		}catch (NoResultException nre) {
			return null;
		}
	}

}